#include <stdio.h>
#include "quick_sort.h"

#define NAME_MAX 32
#define N 20

int cmp(const void *a, const void *b);

typedef struct student_st {
    int id;
    char name[NAME_MAX];
    unsigned int age;
    float score;
} student_st;

int main(int argc, char **argv)
{
    student_st students[N];
    int i;
    char tmp[NAME_MAX];

    for (i = 0; i < N; i++) {
        printf("请输入第%d个学生的信息：\n", i + 1);
        students[i].id = i + 1;
        printf("名称：");
        scanf("%s", students[i].name);
        printf("年龄：");
        scanf("%d", &students[i].age);
        printf("成绩：");
        scanf("%f", &students[i].score);
    }

    quick_sort_optimize(students, N, sizeof(student_st), cmp);

    printf("\n按照成绩降序排序后：\n");
    for (i = 0; i < N; i++) {
        printf("第%d个学生的信息：\n", i + 1);
        printf("id：%d 名称：%s 年龄：%d 成绩：%.2f\n", \
                students[i].id, students[i].name, students[i].age, students[i].score);
    }

    return 0;
}

int cmp(const void *a, const void *b)
{
    float temp = ((*(student_st *) b).score - (*(student_st *) a).score);
    if (temp > 0)
        return 1;
    if (temp < 0)
        return -1;
    return ((*(student_st *) a).id - (*(student_st *) b).id);
}
